package io.github.wjehovah.yyn.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;

import java.text.DecimalFormat;
import java.util.List;

import io.github.wjehovah.yyn.R;

import io.github.wjehovah.yyn.bean.SubscriptBean.SubscriptDetail;

/**
 * 显示订阅列表的Adapter
 * Created by yingchen on 2015/8/10.
 */
public class SubscriptAdapter extends BaseAdapter implements View.OnClickListener{
    private List<SubscriptDetail> def_tags_data;
    private List<SubscriptDetail> rec_tags_data;
    private Context context;
    //private LayoutInflater mLayoutInflater;
    private BitmapUtils bitmapUtils;

    private DecimalFormat decimalFormat;

    public SubscriptAdapter( List<SubscriptDetail> def_tags_data,
                             List<SubscriptDetail> rec_tags_data,
                             Context context){
        this.def_tags_data = def_tags_data;
        this.rec_tags_data = rec_tags_data;
        this.context = context;
       // mLayoutInflater = LayoutInflater.from(context);

        bitmapUtils = new BitmapUtils(context);

        decimalFormat=new DecimalFormat(".0");
    }

    @Override
    public int getCount() {
        return def_tags_data.size()+rec_tags_data.size()+2;
    }

    @Override
    public Object getItem(int position) {
       if(position == 0||position==rec_tags_data.size()+1){
            return 0;
        }else if(position>0&&position<rec_tags_data.size()+1){
            return rec_tags_data.get(position-1);
        }else{
            return def_tags_data.get(position-rec_tags_data.size()-2);
        }

    }



    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position==0||position==rec_tags_data.size()+1){
            //LinearLayout tagView = (LinearLayout) mLayoutInflater.inflate(R.layout.title_subscript,null);
            LinearLayout tagView = (LinearLayout) View.inflate(context,R.layout.title_subscript,null);
            TextView tvTag = (TextView) tagView.findViewById(R.id.tv_subscript_item_tag);
            TextView tvMore = (TextView) tagView.findViewById(R.id.tv_subscript_item_more);

            if(position == 0){
                tvTag.setText("推荐标签");
                tvMore.setVisibility(View.VISIBLE);
            }else{
                tvTag.setText("已订阅标签");
                tvMore.setVisibility(View.GONE);
            }
            return tagView;
        }

        ViewHolder holder = null;
        if(convertView ==null || convertView instanceof LinearLayout){
            holder = new ViewHolder();
            //convertView = mLayoutInflater.inflate(R.layout.subscript_item,null);

            convertView = View.inflate(context,R.layout.subscript_item,null);
            holder.ivSubscriptIcon = (ImageView) convertView.findViewById(R.id.iv_item_subscript_icon);
            holder.tvSubscripyName = (TextView) convertView.findViewById(R.id.tv_item_subscript_name);
            holder.tvSubscriptCount = (TextView) convertView.findViewById(R.id.tv_item_subscript_count);
            holder.btnSubscript = (Button) convertView.findViewById(R.id.btn_item_subscript);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        //获取对应位置的数据
        SubscriptDetail subscriptDetail  = (SubscriptDetail) this.getItem(position);

        Log.e("TAG", "subscriptDetail===" + subscriptDetail.toString());

        bitmapUtils.display(holder.ivSubscriptIcon, subscriptDetail.image_list);
        holder.tvSubscripyName.setText(subscriptDetail.theme_name);
        holder.tvSubscriptCount.setText(decimalFormat.format(Float.parseFloat(subscriptDetail.sub_number) / 10000) + "万人订阅");

        if(position>0&&position<rec_tags_data.size()+1){
            holder.btnSubscript.setText("+订阅");
        }else{
            holder.btnSubscript.setText("取消订阅");
        }

        holder.btnSubscript.setTag(position);
        holder.btnSubscript.setOnClickListener(this);

        return convertView;
    }



    static  class ViewHolder{
        public ImageView ivSubscriptIcon;
        public TextView tvSubscripyName;
        public TextView tvSubscriptCount;
        public Button  btnSubscript;
    }

    /**
     * 点击订阅的按钮
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_item_subscript:
                int position = (int) v.getTag();
                Toast.makeText(context,"Button position ="+position,Toast.LENGTH_SHORT).show();
        }
    }
}
